/**
 * @author rxliuli
 */
const app = (() => {
    const {
        DateFormatter: {
            dateFormatter,
        },
        wait,
        AsyncArray,
        sortBy,
        compare,
        arrayToMap,
    } = rx
    return new BaseValidateVue({
        el: '#app',
        data() {
            return {
                form: new BaseCustomer({
                    officer: '',
                    userType: 1,
                    unitNature: '',
                    idType: '',
                }),
                /**
                 * 域名信息列表
                 */
                baseServiceDomainSet: new Set(),
                domain: null,
                currentSelectDomainList: [],
                baseHousesHoldInfo: new BaseHousesHoldInfo({
                    frameInfoId: '',
                    userType: 1,
                    setMode: 1,
                    businessType: 1,
                    virtualHostName: '',
                    virtualHostState: '',
                    virtualHostType: '',
                    virtualHostAddress: '',
                    virtualHostManagement: '',
                }),
                baseServiceInfo: new BaseServiceInfo({
                    setMode: '',
                    businessType: 1,
                    regType: '',
                    serviceContentSet: [],
                }),
                baseIpTransInfoSet: new Set([new BaseIpTransInfo()]),
                officerList: [],
                provinceCityArea: {
                    provinceMap: new Map(),
                    cityMap: new Map(),
                    areaMap: new Map(),
                },
                serviceContentShow: false,
                domainListShow: false,
                resOtherValidator: false,
                ruleList: [
                    new ValidateObject({
                        ruleName: 'checkIpArr',
                        validate: this.checkIpArrValidate,
                    }),
                    new ValidateObject({
                        ruleName: 'checkPrivateIpArr',
                        validate: this.checkPrivateIpArrValidate,
                    }),
                ],
                baseOperCompanyList: [],
                baseFrameInfoMap: new Map(),
                vhNameShow: false,
                vhTypeShow: false,
                vhAddressShow: false,
                vhManagementShow: false,
                virtualHostValidator: false,
                houseId: '',
            }
        },
        watch: {
            'baseServiceInfo.serviceContentSet': function (val) {
                if (val && val.length > 0) {
                    this.serviceContentShow = false;
                    this.baseServiceInfo.serviceContent = val.join(",");
                    return
                }
                this.serviceContentShow = true;
            },
            'baseServiceDomainSet': function (val) {
                this.domainListShow = false
            },
            'form.idcId': async function (val) {
                this.baseHousesHoldInfo.frameInfoId = ''
                if (val !== '') {
                    let bfi = await baseFrameInfoApi.selectListAppendHouseName(val)
                    this.houseId = bfi[0].houseId
                    this.baseFrameInfoMap = arrayToMap(bfi, 'id', 'frameName')
                } else {
                    this.houseId = ''
                    this.baseFrameInfoMap = new Map()
                }
            },
            'baseHousesHoldInfo.virtualHostName': function (val) {
                this.vhNameShow = false
            },
            'baseHousesHoldInfo.virtualHostType': function (val) {
                this.vhTypeShow = false
            },
            'baseHousesHoldInfo.virtualHostAddress': function (val) {
                this.vhAddressShow = false
            },
            'baseHousesHoldInfo.virtualHostManagement': function (val) {
                this.vhManagementShow = false
            },
        },
        methods: {
            createValidateList() {
                return this.ruleList;
            },
            async checkIpArrValidate(internetEndIp, args) {
                const baseIpTransInfo = ([...this.baseIpTransInfoSet]).filter(BaseIpTransInfo => BaseIpTransInfo.internetEndIp === internetEndIp)
                const {internetStartIp = ''} = baseIpTransInfo[0];
                const fn = val => val.split('.').slice(0, 3)
                return compare(fn(internetStartIp), fn(internetEndIp))
            },
            async checkPrivateIpArrValidate(privateEndIp, args) {
                const baseIpTransInfo = ([...this.baseIpTransInfoSet]).filter(BaseIpTransInfo => BaseIpTransInfo.privateEndIp === privateEndIp)
                const {privateStartIp = ''} = baseIpTransInfo[0];
                const fn = val => val.split('.').slice(0, 3)
                return compare(fn(privateStartIp), fn(privateEndIp))
            },
            /**
             * 添加域名信息
             */
            addDomain() {
                if (!rx.stringValidator.isDomain(this.domain)) {
                    return
                }
                const set = new Set(this.baseServiceDomainSet)
                if (([...set]).some(baserServiceDomain => baserServiceDomain.domain === this.domain)) {
                    return
                }
                if (this.domain != null && this.domain != undefined && this.domain != '') {
                    set.add({
                        domain: this.domain,
                    })
                    this.baseServiceDomainSet = set
                    this.domain = null
                }
            },
            /**
             * 删除域名
             */
            deleteDomain() {
                this.baseServiceDomainSet = new Set(
                    ([...this.baseServiceDomainSet])
                        .filter(baseServiceDomain => !this.currentSelectDomainList.includes(baseServiceDomain.domain)),
                )
            },
            /**
             * 添加 ip 地址转换信息
             */
            addBaseIpAddressInfo() {
                this.baseIpTransInfoSet = new Set(this.baseIpTransInfoSet).add(new BaseIpTransInfo())
            },
            deleteBaseIpTransInfo(index) {
                this.baseIpTransInfoSet = new Set(Array.from(this.baseIpTransInfoSet.values())
                    .filter((_, i) => i !== index))
            },
            /**
             * 添加基本用户
             */
            async confirmAddBaseCustomer() {
                if (this.form.userType === 1) {
                    if (this.baseServiceInfo.serviceContentSet.length === 0) {
                        this.serviceContentShow = true;
                        this.resOtherValidator = true
                    }
                    if (this.baseServiceDomainSet.size === 0) {
                        this.domainListShow = true;
                        this.resOtherValidator = true
                    }
                    if (this.baseHousesHoldInfo.virtualHostName !== '' || this.baseHousesHoldInfo.virtualHostType !== '' || this.baseHousesHoldInfo.virtualHostAddress !== '' || this.baseHousesHoldInfo.virtualHostManagement !== '') {
                        if (this.baseHousesHoldInfo.virtualHostName === '') {
                            this.vhNameShow = true
                            this.virtualHostValidator = true
                        }
                        if (this.baseHousesHoldInfo.virtualHostType === '') {
                            this.vhTypeShow = true
                            this.virtualHostValidator = true
                        }
                        if (this.baseHousesHoldInfo.virtualHostAddress === '') {
                            this.vhAddressShow = true
                            this.virtualHostValidator = true
                        }
                        if (this.baseHousesHoldInfo.virtualHostManagement === '') {
                            this.vhManagementShow = true
                            this.virtualHostValidator = true
                        }
                    }
                }
                if (!await this.$validator.validateAll() || this.resOtherValidator || this.virtualHostValidator) {
                    this.resOtherValidator = false
                    this.virtualHostValidator = false
                    rxPrompt.dangerMsg("输入存在错误！");
                    return
                }

                if (await ajaxOption({
                    res: baseCustomerApi.add({
                        ...this.form,
                        baseServiceDomainList: [...this.baseServiceDomainSet],
                        baseHousesHoldInfo: {
                            ...this.baseHousesHoldInfo,
                            distributeTime: dateFormatter.format(this.baseHousesHoldInfo.distributeTime),
                        },
                        baseServiceInfo: {
                            ...this.baseServiceInfo,
                            registerDate: dateFormatter.format(this.baseServiceInfo.registerDate),
                            expireDate: dateFormatter.format(this.baseServiceInfo.expireDate),
                        },
                        baseIpTransInfoList: [...this.baseIpTransInfoSet],
                        serviceRegTime: dateFormatter.format(this.form.serviceRegTime),
                        houseId: this.houseId
                    }),
                    danger: '新增用户信息失败',
                    success: '新增用户信息成功，即将返回列表页面',
                })) {
                    await wait(1000)
                    forward('/basic-data/admin_user_info')
                }
            },
        },
        computed: {
            fieldFormatter() {
                return fieldFormatter
            },
            provinceMap() {
                return this.provinceCityArea.provinceMap
            },
            cityMap() {
                return this.provinceCityArea.cityMap.get(this.baseServiceInfo.houseProvince) || new Map()
            },
            areaMap() {
                return this.provinceCityArea.areaMap.get(this.baseServiceInfo.houseCity) || new Map()
            },

        },
        async mounted() {
            this.baseOperCompanyList = await baseOperCompanyApi.selectList()
            this.officerList = await baseHouseOfficerApi.selectList()
            this.provinceCityArea = await provinceCityAreaApi.selectMap()
        },
    })
})()